home *** CD-ROM | disk | FTP | other *** search
- Path: crl.crl.com!not-for-mail
- From: bobfry@crl.com (Robert Fry)
- Newsgroups: comp.lang.c
- Subject: Re: Simple i/o for my class
- Date: 30 Jan 1996 08:05:38 -0800
- Organization: CRL Dialup Internet Access
- Message-ID: <4elfki$jnd@crl.crl.com>
- References: <4ekjql$5jl@nnrp1.news.primenet.com>
- NNTP-Posting-Host: crl.com
-
- mcoplea@primenet.com (Marty R. Coplea) writes:
-
- <snipping most of the question>
-
- >It seems to take the input OK, but always returns a '0'
-
- >int
- >main(void)
- >{
- > float avar, hvar; /*float variables(avar=At-bats,hvar=Hits */
- > int Hitsint, Atbatsint; /* Integers with Hits and At Bats */
- > float Avg; /* Results Batting Average */
-
- > /* Prompt the user to input the data */
- > printf("Enter the number of at-bats followed by hits: ");
- > scanf("%f %f", &avar, &hvar);
-
- > /* Convert to integers */
- > Atbatsint = avar;
- > Hitsint = hvar;
-
- > /* Calculate the Batting Average */
- > Avg = Hitsint/Atbatsint;
-
- This line probably doesn't do what you're expecting. If you look at it
- with a debugger, you'll notice that Avg gets assigned the value '0',
- here. The problem is that you're doing integer arithmetic (and here's a
- problem for you to solve for yourself: why?). You can either get rid of
- the integer variables (since they do little but clutter the program) or
- cast the dividend and divisor to float before calculating the average.
-
- <skipping the rest>
-
- I hope this helps.
- Bob
-